//
//  StatusViewModel.swift
//  WeiBoSwift
//
//  Created by 吴斌 on 2022/10/9.
//

import UIKit

class StatusViewModel: NSObject {
    // MARK: - 定义属性
    var status : Status?
    
    // MARK: - 对数据处理的属性
    var sourceText : String?
    var createAtText : String?
    var verifiedImage : UIImage?
    var vipImage : UIImage?
    var profileURL : URL?
    
    // MARK: - 自定义构造函数
    init(status : Status) {
        self.status = status
        
        // 1.对来源数据处理
        if let source = status.source, source != "" {
            // 1.1获取起始位置和截取的长度
            let startIndex = (source as NSString).range(of: ">").location + 1
            let length = (source as NSString).range(of: "</").location - startIndex
            
            // 1.2截取字符串
            sourceText = (source as NSString).substring(with: NSRange(location:startIndex, length: length))
        }
        
        // 2.处理创建时间
        if let created_at = status.created_at {
            createAtText = Date.createDateString(createAtStr: created_at)
        }

        // 3. 处理认证
        let verifiedType = status.user?.verified_type ?? -1
        switch verifiedType {
        case 0:
            verifiedImage = UIImage(named: "logo")
        case 2, 3, 5:
            verifiedImage = UIImage(named: "logo")
        case 220:
            verifiedImage = UIImage(named: "logo")
        default:
            verifiedImage = nil
        }
        
        // 4.处理会员图标
        let mbrank = status.user?.mbrank ?? 0
        if mbrank > 0 && mbrank <= 6 {
            vipImage = UIImage(named: "logo")
        }
        
        // 5.用户头像的处理
        let profileURLString = status.user?.profile_image_url ?? ""
        profileURL = URL(string: profileURLString)
    }
    
}
